package org.unicom.quartz.service;

import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class QuartzService {

    @Autowired
    private Scheduler scheduler;

    /**
     * 启动任务
     *
     * @param jobName
     * @param jobGroup
     * @param className
     * @param cron
     * @throws Exception
     */
    public void startJob(String jobName, String jobGroup, String className, String cron) throws Exception {
        Class<Job> jobClass = null;
        try {
            jobClass = (Class<Job>) Class.forName(className);//获取任务执行类
        } catch (ClassNotFoundException e) {
            throw new Exception("任务类不存在");
        }
        //创建job，指定job名称和分组
        JobDetail jobDetail = JobBuilder.newJob(jobClass).withIdentity(jobName, jobGroup).build();
        //创建表达式工作计划
        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(cron);
        //创建触发器
        CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity(jobName, jobGroup)
                .withSchedule(cronScheduleBuilder).build();
        scheduler.scheduleJob(jobDetail, cronTrigger);
    }

    /**
     * 修改定时任务执行时间
     *
     * @param jobName
     * @param jobGroup
     * @param cron     新的时间
     * @throws Exception
     */
    public void modifyJob(String jobName, String jobGroup, String cron) throws Exception {
        TriggerKey triggerKey = new TriggerKey(jobName, jobGroup);
        CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
        String oldCron = trigger.getCronExpression();
        if (!oldCron.equals(cron)) {
            CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity(jobName, jobGroup)
                    .withSchedule(CronScheduleBuilder.cronSchedule(cron)).build();
            Date date = scheduler.rescheduleJob(triggerKey, cronTrigger);
            if (date == null) {
                throw new Exception("修改定时任务执行时间报错");
            }
        }
    }

    /**
     * 暂停某个定时任务（任务恢复后，暂停时间段内未执行的任务会继续执行，如暂停时间段内有2次，则会执行2次）
     *
     * @param jobName
     * @param jobGroup
     * @throws Exception
     */
    public void pauseJob(String jobName, String jobGroup) throws Exception {
        JobKey jobKey = new JobKey(jobName, jobGroup);
        JobDetail jobDetail = scheduler.getJobDetail(jobKey);
        if (jobDetail == null) {
            return;
        }
        scheduler.pauseJob(jobKey);
    }

    /**
     * 恢复某个定时任务
     *
     * @param jobName
     * @param jobGroup
     * @throws Exception
     */
    public void resumeJob(String jobName, String jobGroup) throws Exception {
        JobKey jobKey = new JobKey(jobName, jobGroup);
        JobDetail jobDetail = scheduler.getJobDetail(jobKey);
        if (jobDetail == null) {
            return;
        }
        scheduler.resumeJob(jobKey);
    }

    /**
     * 删除某个定时任务
     *
     * @param jobName
     * @param jobGroup
     * @throws Exception
     */
    public void deleteJob(String jobName, String jobGroup) throws Exception {
        JobKey jobKey = new JobKey(jobName, jobGroup);
        JobDetail jobDetail = scheduler.getJobDetail(jobKey);
        if (jobDetail == null) {
            return;
        }
        scheduler.deleteJob(jobKey);
    }
}
